<template>
  <HeaderNav></HeaderNav>
  <div class="container_down">
    <div class="titbox">
      <div class="title">{{ currentLang.title }}</div>
      <div class="sub">
        {{ currentLang.subtitle }}
      </div>
    </div>

    <div class="down_wrap">
      <div class="downbox">
        <div class="down_btn" @click="handleDown('left')">{{ currentLang.keyboardFirmwareDownload }}</div>
        <div class="bigpic">
          <img class="pc_pic" src="./pc.png" alt="" />
          <img class="soft_pic" :src="leftPic" alt="">
        </div>
        <div class="tipsoptbox">
          <div class="item">
            <img class="icon" src="../assets/updataPic/icon1.png" alt="" />
            <span class="text">{{ currentLang.steps.step1 }}</span>
          </div>
          <span class="line"></span>
          <div class="item">
            <img class="icon" src="../assets/updataPic/icon2.png" alt="" />
            <span class="text">{{ currentLang.steps.step2Keyboard }}</span>
          </div>
          <span class="line"></span>
          <div class="item">
            <img class="icon" src="../assets/updataPic/icon3.png" alt="" />
            <span class="text">{{ currentLang.steps.step3Keyboard }}</span>
          </div>
          <span class="line"></span>
          <div class="item">
            <img class="icon" src="../assets/updataPic/icon4.png" alt="" />
            <span class="text">{{ currentLang.steps.step4Keyboard }}</span>
          </div>
        </div>

        <div class="descbox">
          <div class="t">{{ currentLang.keyboardFirmware.updateContent }}</div>
          <div class="t">
            {{ currentLang.keyboardFirmware.content1 }}
          </div>
          <div class="t t2">{{ currentLang.keyboardFirmware.updateNotice }}</div>
          <div class="t">
            {{ currentLang.keyboardFirmware.notice1 }}
          </div>
          <div class="t">
            {{ currentLang.keyboardFirmware.notice2 }}
          </div>
          <div class="t">
            {{ currentLang.keyboardFirmware.notice4 }}
          </div>
          <div class="t">{{ currentLang.keyboardFirmware.notice5 }}</div>
        </div>
      </div>

      <div class="downbox" v-if="layout == 'default'">
        <div class="down_btn" @click="handleDown('right')">{{ currentLang.dongleFirmwareDownload }}</div>
        <div class="bigpic">
          <img class="pc_pic" src="./pc.png" alt="" />
          <img class="soft_pic" :src="rightPic" alt="">
        </div>
        <div class="tipsoptbox">
          <div class="item">
            <img class="icon" src="../assets/updataPic/icon1.png" alt="" />
            <span class="text">{{ currentLang.steps.step1 }}</span>
          </div>
          <span class="line"></span>
          <div class="item">
            <img class="icon" src="../assets/updataPic/icon2.png" alt="" />
            <span class="text">{{ currentLang.steps.step2Dongle }}</span>
          </div>
          <span class="line"></span>
          <div class="item">
            <img class="icon" src="../assets/updataPic/icon3.png" alt="" />
            <span class="text">{{ currentLang.steps.step3Dongle }}</span>
          </div>
          <span class="line"></span>
          <div class="item">
            <img class="icon" src="../assets/updataPic/icon4.png" alt="" />
            <span class="text">{{ currentLang.steps.step4Dongle }}</span>
          </div>
        </div>

        <div class="descbox">
          <div class="t">{{ currentLang.dongleFirmware.updateContent }}</div>
          <div class="t">
            {{ currentLang.dongleFirmware.content1 }}
          </div>
          <div class="t t2">{{ currentLang.dongleFirmware.updateNotice }}</div>
          <div class="t">
            {{ currentLang.dongleFirmware.notice1 }}
          </div>
          <div class="t">
            {{ currentLang.dongleFirmware.notice2 }}
          </div>
          <div class="t">{{ currentLang.dongleFirmware.notice3 }}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import HeaderNav from "../comps/HeaderNav.vue";
import { useI18n } from 'vue-i18n';
import { computed,onMounted ,ref} from 'vue';
import { getUpdataJson } from "../utils/tools";
import { message } from 'ant-design-vue';
import { useRoute } from 'vue-router';
const route = useRoute();
const updataList = ref([])
onMounted(async()=>{
  updataList.value= await getUpdataJson()
})

const updataDeviceId = route.query.type || 'mouse_s9' 
const layout = route.query.layout || 'default'

// 使用 import.meta.glob 加载图片
const imageModules = import.meta.glob('./*/{left,right}.png', { eager: true });
const leftPic = imageModules[`./${updataDeviceId}/left.png`]?.default || `./${updataDeviceId}/left.png`;
const rightPic = imageModules[`./${updataDeviceId}/right.png`]?.default || `./${updataDeviceId}/right.png`;

// 使用国际化
const { locale } = useI18n();

// 根据当前语言选择对应的文本
const currentLang = computed(() => import.meta.glob('./*/{zh,en}.js', { eager: true })[`./${updataDeviceId}/${locale.value === 'en' ? 'en' : 'zh'}.js`].default);

const handleDown = (type) => {  
  const url = updataList.value.find(item => item.updataDeviceId === updataDeviceId)?.downUrl[type]
  // https://hubtest.eweadn.cn/hfd87/updata/x87/X87_87K_RGB_HFD2201KBA_V1.11_20250711_0x87C9.exe
  
  // 获取URL中的文件名（包括后缀）
  const getFileNameFromUrl = (url) => {
    if (!url) return '';
    // 获取URL路径的最后一部分
    const pathParts = url.split('/');
    return pathParts[pathParts.length - 1];
  };
  
  if (url) {
    console.log('downUrl:',url)
    const fileName = getFileNameFromUrl(url);
    console.log('文件名:', fileName); // 输出: X87_87K_RGB_HFD2201KBA_V1.11_20250711_0x87C9.exe
    
    const link = document.createElement('a'); 
    link.href = url;
    link.download = fileName; // 使用提取的文件名
    link.style.display = 'none';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  }else{
    message.error('下载地址不存在')
  }
};
</script>
<style scoped>
.container_down {
  color: #fff;
  display: flex;
  flex-direction: column;
}
.titbox {
  margin-top: 100px;
  text-align: center;
}
.title {
  font-size: 40px;
  font-weight: bold;
}
.sub {
  color: #999;
  margin-top: 10px;
}
.downbox {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.down_btn {
  min-width: 240px;
  padding: 0 20px;
  height: 40px;
  border-radius: 20px;
  background: #ff5019;
  color: #fff;
  text-align: center;
  line-height: 40px;
  margin-bottom: 24px;
  margin-top: 60px;
  cursor: pointer;
}
.bigpic{
  position: relative;
}
.bigpic .pc_pic {
  width: 560px;
}
.tipsoptbox {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 33px;
}
.tipsoptbox .item {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.tipsoptbox .line {
  width: 30px;
  height: 3px;
  background-color: #898989;
  margin-top: -20px;
}
.tipsoptbox .icon {
  width: 30px;
  height: 30px;
  margin-bottom: 12px;
}
.tipsoptbox .text {
  font-size: 10px;
  color: #fff;
  text-align: center;
}
.descbox {
  color: #fff;
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  margin-top: 26px;
  margin-bottom: 100px;
  min-height: 204px;
  max-width: 680px;
}
.descbox .t {
  line-height: 1.5;
  font-size: 12px;
  white-space: normal;
  overflow-wrap: break-word;
  word-break: normal;
}
.t2 {
  margin-top: 20px;
}
.down_wrap{
    display: flex;
    justify-content: space-evenly;
}
.soft_pic{
  width: 200px;
  position: absolute;
  top:50%;
  left:50%;
  transform: translate(-50%,-50%);
}
</style>
